<template>
  <div class="countBox">
    <div class="countWarn">
      <div class="mainCount">
        <p>小区报警总数</p>
        <!-- 分类展示 -->
        <div class="warn-legend">
          <div v-for="(item, i) in alarmList" :key="i" class="list"><span :style="{background: chooseColor(i)}"></span><i>{{ item.moduleEnum.name }}</i></div>
        </div>
        <!-- 分类占比 -->
        <ul class="type-length" :style="{width:typeWidth + 'px'}">
          <li v-for="(item, i) in alarmList" :key="i" :style="{background: chooseColor(i), width: chooseWidth(item.eventCount)}"></li>
        </ul>
      </div>
      <div class="allCountData">{{allCount}}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'countWarn',
  props: ['areaId'],
  data () {
    return {
      alarmList: [],
      warnData: [],
      typeWidth: ''
    }
  },
  computed: {
    // 总数
    allCount () {
      let count = 0
      for (let i in this.alarmList) {
        let item = this.alarmList[i]
        count += item.eventCount
      }
      return count
    }
  },
  watch: {
    areaId (val) {
      this.initAlarmList(val)
    }
  },
  mounted () {
    this.typeWidth = $('.countBox').width() - 36
    this.initAlarmList(this.areaId)
  },
  methods: {
    initAlarmList (deptId) {
      let _this = this

      if (!deptId) {
        return
      }

      let url = this.$base.getUrl('summaryEventByAreaId')

      let params = {
        url: url,
        data: {
          'areaId': deptId
        }
      }
      // 获取实际报警数据
      this.$store.dispatch('postForm', params).then((res) => {
        console.log(res.result, 33333)
        _this.alarmList = res.result
      })
    },
    chooseColor (i) {
      return this.$base.getConst('common', 'colorMore')[i]
    },
    chooseWidth (i) {
      let boxW = $('.countBox').width() - 40
      let l = Object.getOwnPropertyNames(this.alarmList).length // 获取种类数
      let w = Math.floor((i / this.allCount) * (boxW - (l * 2)))
      return w + 'px'
    }
  }
}
</script>
<style scoped>
  .countBox {
    width: 100%;
  }

  .countWarn {
    position: relative;
    background: rgba(43, 54, 72, 0.95);
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    margin-bottom: 10px;
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
  }

  .warn-legend {
    width: 100%;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .warn-legend .list {
    padding-bottom: 10px;
  }

  .mainCount p {
    color: #fff;
    width: 77%;
    margin-bottom: 15px;
  }

  .type-length {
    /* width: 100%; */
    padding: 4px 0 8px;
    /*height: 10px;*/
  }

  .type-length li {
    list-style: none;
    float: left;
    height: 10px;
    margin-right: 2px;
  }

  .mainCount i {
    font-style: normal;
    width: 42%;
    opacity: 0.8;
    font-size: 12px;
  }

  .mainCount span {
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    border-radius: 2px;
    width: 8px;
    height: 8px;
    float: left;
    margin-right: 9px;
  }

  .mainCount .warn-legend div {
    width: 34%;
    line-height: 10px;
    color: #fff;
    /*margin-bottom: 7px;*/
    margin-right: 30px;
  }

  .allCountData {
    color: #fff;
    font-size: 36px;
    position: absolute;
    right: 20px;
    top: 0;
    z-index: 9;
  }
</style>
